﻿module WingBeats.Xml.NodeTests
open   Xunit
open   WingBeats
open   WingBeats.Xml

type TestAttribute = FactAttribute

let (==) o1 o2 = Assert.Equal(o1, o2)
let (!=) o1 o2 = Assert.NotEqual (o1, o2)


[<Test>]
let ``<Compare equal TagPairNodes - empty divs>`` () =
    let node1 = TagPairNode("div", [], [])
    let node2 = TagPairNode("div", [], [])
    node1 == node2

[<Test>]
let ``<Compare equal TagPairNodes - divs with equal attributes in equal order>`` () =
    let node1 = TagPairNode("div", [ ("class", "test"); ("id", "test-div") ], [])
    let node2 = TagPairNode("div", [ ("class", "test"); ("id", "test-div") ], [])
    node1 == node2

[<Test>]
let ``<Compare equal TagPairNodes - divs with equal attributes in different order>`` () =
    let node1 = TagPairNode("div", [ ("class", "test"); ("id", "test-div") ], [])
    let node2 = TagPairNode("div", [ ("id", "test-div"); ("class", "test") ], [])
    node1 == node2
 
[<Test>]
let ``<Compare equal TagPairNodes - divs with equal children>`` () =
    let node1 = TagPairNode("div", 
                           [], 
                           [ TagPairNode ("h1", [], [])
                             TagPairNode ("p", [], []) 
                           ])
    let node2 = TagPairNode("div", 
                       [], 
                       [ TagPairNode ("h1", [], [])
                         TagPairNode ("p", [], []) 
                       ])
    node1 == node2

[<Test>]
let ``<Compare not equal TagPairNodes - empty div and p>`` () =
    let node1 = TagPairNode("div", [], [])
    let node2 = TagPairNode("p", [], [])
    node1 != node2

[<Test>]
let ``<Compare not equal TagPairNodes - divs with different attributes>`` () =
    let node1 = TagPairNode("div", [ ("class", "test"); ("id", "test-div") ], [])
    let node2 = TagPairNode("div", [ ("align", "center"); ("id", "test-div") ], [])
    node1 != node2

[<Test>]
let ``<Compare not equal TagPairNodes - divs with different children>`` () =
    let node1 = TagPairNode("div", 
                       [], 
                       [ TagPairNode ("h1", [], [])
                         TagPairNode ("p", [], []) 
                       ])
    let node2 = TagPairNode("div", 
                   [], 
                   [ TagPairNode ("p", [], []) 
                     TagPairNode ("div", [], [])
                   ])
    node1 != node2

[<Test>]
let ``<Compare not equal TagPairNodes - divs with equal children in different order>`` () =
    let node1 = TagPairNode("div", 
                       [], 
                       [ TagPairNode ("h1", [], [])
                         TagPairNode ("p", [], []) 
                       ])
    let node2 = TagPairNode("div", 
                   [], 
                   [ TagPairNode ("p", [], []) 
                     TagPairNode ("h1", [], [])
                   ])
    node1 != node2

[<Test>]
let ``<Compare not equal TagPairNode - div elements, one with placeholder>`` () =
    let node1 = TagPairNode ("div", [], [ Placeholder ("test-holder") ])
    let node2 = TagPairNode ("div", [], [])
    node1 != node2

[<Test>]
let ``<Compare not equal TagPairNode - div elements, one with LiteralText>`` () =
    let node1 = TagPairNode ("div", [], [ LiteralText ("lorem ipsum") ])
    let node2 = TagPairNode ("div", [], [])
    node1 != node2

[<Test>]
let ``<Compare not equal TagPairNode - div elements, one with Text>`` () =
    let node1 = TagPairNode ("div", [], [ Text ("lorem ipsum") ])
    let node2 = TagPairNode ("div", [], [])
    node1 != node2

[<Test>]
let ``<Compare not equal TagPairNode - div elements, one with a SelfClosingNode>`` () =
    let node1 = TagPairNode ("div", [], [ SelfClosingNode ("br", []) ])
    let node2 = TagPairNode ("div", [], [])
    node1 != node2

[<Test>]
let ``<Compare not equal TagPairNode - div elements, one with a TagPairNode>`` () =
    let node1 = TagPairNode ("div", [], [ TagPairNode ("div", [], []) ])
    let node2 = TagPairNode ("div", [], [])
    node1 != node2

[<Test>]
let ``<Compare not equal TagPairNode - div elements, one with an attribute>`` () =
    let node1 = TagPairNode ("div", [("class", "test")], [])
    let node2 = TagPairNode ("div", [], [])
    node1 != node2

[<Test>]
let ``<Compare not equal TagPairNode - div elements, one with a NoNode>`` () =
    let node1 = TagPairNode ("div", [], [ NoNode ])
    let node2 = TagPairNode ("div", [], [])
    node1 != node2

[<Test>]
let ``<Compare not equal TagPairNode - div elements, children is equal, but not grandChildren>`` () =
    let node1 = TagPairNode("div", 
                       [], 
                       [ TagPairNode ("h1", [], [ Text ("Lorem ipsum") ])
                         TagPairNode ("p", [], [ TagPairNode("span", [], [ Text ("Lorem ipsum") ]) ]) 
                       ])
    let node2 = TagPairNode("div", 
                       [], 
                       [ TagPairNode ("h1", [], [ Text ("Muspi merol") ])
                         TagPairNode ("p", [], [ TagPairNode("span", [], [ Text ("Muspi merol") ]) ]) 
                       ])
    node1 != node2 


[<Test>]
let ``<Compare equal SelfClosingNode - br tags without attributes>`` () =
    let node1 = SelfClosingNode ("br", [])
    let node2 = SelfClosingNode ("br", [])
    node1 == node2

[<Test>]
let ``<Compare equal SelfClosingNode - a elements with equal attributes in equal order>`` () =
    let node1 = SelfClosingNode ("a", [ ("href", "http://www.example.com"); ("title", "lorem ipsum") ])
    let node2 = SelfClosingNode ("a", [ ("href", "http://www.example.com"); ("title", "lorem ipsum") ])
    node1 == node2

[<Test>]
let ``<Compare equal SelfClosingNode - a elements with equal attributes in different order>`` () =
    let node1 = SelfClosingNode ("a", [ ("title", "lorem ipsum"); ("href", "http://www.example.com") ])
    let node2 = SelfClosingNode ("a", [ ("href", "http://www.example.com"); ("title", "lorem ipsum") ])
    node1 == node2

[<Test>]
let ``<Compare not equal SelfClosingNode - br and hr tag without attributes>`` () =
    let node1 = SelfClosingNode ("br", [])
    let node2 = SelfClosingNode ("hr", [])
    node1 != node2

[<Test>]
let ``<Compare not equal SelfClosingNode - brs, one with an attribute>`` () =
    let node1 = SelfClosingNode ("br", [])
    let node2 = SelfClosingNode ("br", [("class", "test")])
    node1 != node2

[<Test>]
let ``<Compare not equal SelfClosingNode - a elements with not equal attributes>`` () =
    let node1 = SelfClosingNode ("a", [ ("href", "http://www.example.com"); ("title", "lorem ipsum") ])
    let node2 = SelfClosingNode ("a", [ ("href", "http://www.another-example.com"); ("id", "lorem ipsum") ])
    node1 != node2

[<Test>]
let ``<Compare equal Text nodes>`` () =
    Text ("lorem ipsum") == Text ("lorem ipsum")

[<Test>]
let ``<Compare not equal Text nodes>`` () =
    Text ("lorem ipsum") != Text ("")

[<Test>]
let ``<Compare equal LiteralText nodes>`` () =
    LiteralText ("lorem ipsum") == LiteralText("lorem ipsum")

[<Test>]
let ``<Compare not equal LiteralText nodes>`` () =
    LiteralText ("lorem ipsum") != LiteralText("lorem")

[<Test>]
let ``<Compare Text and LiteralText nodes with equal text>`` () =
    LiteralText ("lorem ipsum") != Text ("lorem ipsum")

[<Test>]
let ``<Compare Text and Placeholder nodes with equal text>`` () =
    Placeholder ("lorem ipsum") != Text ("lorem ipsum")

[<Test>]
let ``<Compare LiteralText and Placeholder nodes with equal text>`` () =
    Placeholder ("lorem ipsum") != LiteralText ("lorem ipsum")

[<Test>]
let ``<Compare equal Placeholders>`` () =
    Placeholder ("test") == Placeholder ("test")

[<Test>]
let ``<Compare not equal Placeholders>`` () =
    Placeholder ("test") != Placeholder ("test2")

[<Test>]
let ``<Compare two NoNodes>`` () = 
    NoNode == NoNode